package com.ys.service.business.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.TpAmazonSeller;
import com.ys.mapper.business.TpAmazonSellerMapper;
import com.ys.service.business.TpAmazonSellerService;
import com.ys.vo.R;

@Service("tpAmazonSellerService")
public class TpAmazonSellerServiceImpl implements TpAmazonSellerService {
	
	@Autowired
	private TpAmazonSellerMapper tpAmazonSellerMapper;
	
	@Override
	public R add(Map<String, Object> params) {
		
		// 验证卖家ID
		if (!params.containsKey("sellerId")) {
			return R.error("卖家ID不能为空");
		}
		String sellerId = params.get("sellerId").toString();
		String vgsbm = params.get("vgsbm").toString();
		if(new TpAmazonSeller().setSellerId(sellerId).setVgsbm(vgsbm).count() > 0) {
			return R.error("该卖家ID和运营公司已存在，不能重复添加");
		}
		
		TpAmazonSeller entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonSeller.class);
		entity.setAuthState(0);
		tpAmazonSellerMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params) {

		TpAmazonSeller entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonSeller.class);
		String sellerId = params.get("sellerId").toString();
		String vgsbm = params.get("vgsbm").toString();
		Long id = Long.valueOf(params.get("id").toString());
		if(new TpAmazonSeller().setSellerId(sellerId).setVgsbm(vgsbm).where("A.id != " + id).count() > 0) {
			return R.error("该卖家ID和运营公司已存在，不能重复添加");
		}
		tpAmazonSellerMapper.update(entity);

		return R.ok(EditDto.closeAndReload());
	}
}
